package com.wxmit.framework.quartz.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wxmit.base.domain.ApiResult;
import com.wxmit.framework.quartz.domain.SysJob;
import com.wxmit.framework.quartz.exception.TaskException;
import com.wxmit.framework.quartz.service.ISysJobService;
import com.wxmit.framework.web.operatelog.Log;
import com.wxmit.framework.web.operatelog.LogModule;
import com.wxmit.framework.web.operatelog.enums.BusinessType;
import com.wxmit.base.validate.AddGroup;
import com.wxmit.base.validate.UpdateGroup;
import com.wxmit.system.base.core.ShushanBaseController;
import com.wxmit.system.base.domain.PageDTO;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.groups.Default;
import java.util.List;

/**
 * 定时任务调度Controller
 * 
 * @author xiong
 * @date 2022-08-11
 */
@RestController
@RequestMapping("/system/job")
@LogModule("定时任务调度")
@Slf4j
public class SysJobController extends ShushanBaseController<ISysJobService,SysJob,Long>{

    public SysJobController(ISysJobService baseService){
        super(baseService,"system:job:");
    }



    @Log(name = "更新任务状态",type = BusinessType.UPDATE)
    @PutMapping("/changeStatus/{id}/{status}")
    public ApiResult changeStatus(@PathVariable Long id,@PathVariable String status) throws SchedulerException {
        return ApiResult.successWithData(baseService.changeStatus(id,status));
    }

    @Log(name = "执行一次定时任务",type = BusinessType.OTHER)
    @PutMapping("/executeOnce/{id}")
    public ApiResult executeOnce(@PathVariable Long id) throws SchedulerException {
        baseService.executeOnce(id);
        return ApiResult.success();
    }


    @Log(name = "新增操作",type = BusinessType.INSERT)
    @PostMapping
    @Override
    public ApiResult add(@RequestBody @Validated({AddGroup.class, Default.class})  SysJob domain) {
        try {
            baseService.insertJob(domain);
        } catch (Exception e) {
            log.error("新增定时任务失败："+e.getMessage(),e);
            return ApiResult.fail("新增定时任务失败");
        }
        return ApiResult.success();
    }

    @Log(name = "更新操作",type = BusinessType.INSERT)
    @PutMapping
    @Override
    public ApiResult update(@RequestBody @Validated({UpdateGroup.class, Default.class})  SysJob domain) {
        try {
            baseService.updateJobById(domain);
        } catch (Exception e) {
            log.error("编辑定时任务失败："+e.getMessage(),e);
            return ApiResult.fail("编辑定时任务失败");
        }
        return ApiResult.success();
    }

    @Log(name = "更新操作",type = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Override
    public ApiResult del(@PathVariable List<Long> ids) {
        try {
            baseService.removeJobByIds(ids);
        } catch (Exception e) {
            log.error("删除定时任务失败："+e.getMessage(),e);
            return ApiResult.fail("删除定时任务失败");
        }
        return ApiResult.success();
    }
}